package com.whl.sm.service.impl.inhabitant;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.inhabitant.AssetsTenantMapper;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.ContractTenancy;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.system.Role;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.ContractTenancyService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.service.system.UserCpRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-14
 */
@Service
public class AssetsTenantServiceImpl extends ServiceImpl<AssetsTenantMapper, AssetsTenant> implements AssetsTenantService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserCpRoleService userCpRoleService;


    @Autowired
    private ContractTenancyService contractTenancyService;


    @Autowired
    private OrgUserService orgUserService;

    @Lazy
    @Autowired
    private ComUnitsService comUnitsService;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public Result bindTenant(AssetsTenant assetsTenant) {
        assetsTenant.setOperator(orgUserService.getCurrentUserId());

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("assets_id",assetsTenant.getAssetsId());
        queryWrapper.ne("is_del",1);

        List<AssetsTenant> hasTenList = getBaseMapper().selectList(queryWrapper);
        if (hasTenList.size()>0){
            return Result.build(444,"该单元已绑定租户！");
        }

        ComUnits linkUnit = comUnitsService.getById(assetsTenant.getAssetsId());
        linkUnit.setUnitStatus(2);
        comUnitsService.update(linkUnit);

        getBaseMapper().insert(assetsTenant);
        Role tenant = roleService.getOne(new QueryWrapper<Role>().eq("role_name","小区租户"));
        UserCpRole insert = new UserCpRole();
        insert.setRoleId(tenant.getId());
        insert.setUserId(assetsTenant.getUserId());
        userCpRoleService.save(insert);
        ContractTenancy insertContract = new ContractTenancy();
        insertContract.setTenantId(assetsTenant.getId());
        insertContract.setValidTime(assetsTenant.getValidTime());
        insertContract.setExpenseId(assetsTenant.getContractExpenseType());
        contractTenancyService.update(insertContract);
        return Result.ok();
    }
}
